//请求前缀
var baseURL = "https://oxtoken.io";

var vm = new Vue({
    el: '#app',
    data: {
        step:1
        ,phone:''
        ,vercode:''
        ,countDown: false
        ,password:''
        ,repassword:''
    },
    methods: {
        getVerCode: function () {
            if(!vm.phone){
                this.$dialog.toast({
                    mes: '手机号不能为空',
                    icon: 'error',
                    timeout: 1500
                });
                return;
            }else if(!/1[3|4|5|6|7|8|9]\d{9}/.test(vm.phone)){
                this.$dialog.toast({
                    mes: '手机号格式错误',
                    icon: 'error',
                    timeout: 1500
                });
                return;
            }else if(vm.phone.length != 11){
                this.$dialog.toast({
                    mes: '您输的不是手机号',
                    icon: 'error',
                    timeout: 1500
                });
                return;
            }

            var pt = this;
            pt.$dialog.loading.open('发送中...');

            $.ajax({
                type: "POST",
                url: baseURL + "/api/v1/sms/send/sm",
                data: JSON.stringify({"mobile":vm.phone,"action":"find-password"}),
                contentType: "application/json",
                dataType:  "json",
                success: function(r){
                    pt.$dialog.loading.close();
                    pt.countDown = true;

                    if(r.code == 1){
                        pt.countDown = true;
                        pt.$dialog.toast({
                            mes: '发送成功',
                            icon: 'success',
                            timeout: 1500
                        });
                    }else {
                        pt.$dialog.toast({
                            mes: '发送失败',
                            icon: 'error',
                            timeout: 1500
                        });
                    }
                },
                error:function(e){
                    pt.$dialog.loading.close();
                    if(typeof e.responseJSON != 'undefined'){
                        pt.$dialog.toast({
                            mes: e.responseJSON.message,
                            icon: 'error',
                            timeout: 1500
                        });
                    }else {
                        pt.$dialog.toast({
                            mes: '服务异常',
                            icon: 'error',
                            timeout: 1500
                        });
                    }
                }
            });
        },
        nextStep: function () {
            if(!vm.phone){
                this.$dialog.toast({
                    mes: '手机号不能为空',
                    icon: 'error',
                    timeout: 1500
                });
                return;
            }else if(!/1[3|4|5|6|7|8|9]\d{9}/.test(vm.phone)){
                this.$dialog.toast({
                    mes: '手机号格式错误',
                    icon: 'error',
                    timeout: 1500
                });
                return;
            }else if(vm.phone.length != 11){
                this.$dialog.toast({
                    mes: '您输的不是手机号',
                    icon: 'error',
                    timeout: 1500
                });
                return;
            }

            if(!vm.vercode){
                this.$dialog.toast({
                    mes: '验证码不能为空',
                    icon: 'error',
                    timeout: 1500
                });
                return;
            }else if(!/^[0-9]+.?[0-9]*$/.test(vm.vercode)){
                this.$dialog.toast({
                    mes: '验证码格式错误',
                    icon: 'error',
                    timeout: 1500
                });
                return;
            }else if(vm.vercode.length != 6){
                this.$dialog.toast({
                    mes: '验证码格式错误',
                    icon: 'error',
                    timeout: 1500
                });
                return;
            }

            var pt = this;
            pt.$dialog.loading.open('验证中...');

            $.ajax({
                type: "POST",
                url: baseURL + "/api/v1/sms/verify/sm",
                data: JSON.stringify({mobile:vm.phone,code: vm.vercode}),
                contentType: "application/json",
                dataType:  "json",
                success: function(r){
                    pt.$dialog.loading.close();
                    if(r.code == 1){
                        vm.step = 2;
                    }else {
                        pt.$dialog.toast({
                            mes: r.message,
                            icon: 'error',
                            timeout: 1500
                        });
                    }
                },
                error:function(e){
                    pt.$dialog.loading.close();
                    if(typeof e.responseJSON != 'undefined'){
                        pt.$dialog.toast({
                            mes: e.responseJSON.code,
                            icon: 'error',
                            timeout: 1500
                        });
                    }else {
                        pt.$dialog.toast({
                            mes: '服务异常',
                            icon: 'error',
                            timeout: 1500
                        });
                    }
                }
            });
        },
        resetPassword: function () {
            if(!vm.password){
                this.$dialog.toast({
                    mes: '请输入密码',
                    icon: 'error',
                    timeout: 1500
                });

                return;
            }

            if(vm.password.length > 16 || vm.password.length < 8 || !/^[0-9a-zA-Z]+$/.test(vm.password)){
                this.$dialog.toast({
                    mes: '请设置8~16位英文+数字',
                    icon: 'error',
                    timeout: 1500
                });

                return;
            }

            if(!vm.repassword){
                this.$dialog.toast({
                    mes: '请确认密码',
                    icon: 'error',
                    timeout: 1500
                });

                return;
            }

            if(vm.repassword != vm.password){
                this.$dialog.toast({
                    mes: '密码输入不一致',
                    icon: 'error',
                    timeout: 1500
                });

                return;
            }

            var pt = this;
            pt.$dialog.loading.open('重置中...');

            $.ajax({
                type: "POST",
                url: baseURL + "/api/v1/ch-pw",
                data: JSON.stringify({mobile:vm.phone,new:vm.password,code: vm.vercode}),
                contentType: "application/json",
                dataType:  "json",
                success: function(r){
                    pt.$dialog.loading.close();

                    if(r.code == 'OK'){
                        pt.$dialog.toast({
                            mes: '重置成功',
                            icon: 'success',
                            timeout: 1500,
                            callback:function () {
                                location.href = 'login.html';
                            }
                        });
                    }else {
                        pt.$dialog.toast({
                            mes: r.code,
                            icon: 'error',
                            timeout: 1500
                        });
                    }
                },
                error:function(e){
                    pt.$dialog.loading.close();
                    if(typeof e.responseJSON != 'undefined'){
                        pt.$dialog.toast({
                            mes: e.responseJSON.code,
                            icon: 'error',
                            timeout: 1500
                        });
                    }else {
                        pt.$dialog.toast({
                            mes: '服务异常',
                            icon: 'error',
                            timeout: 1500
                        });
                    }
                }
            });
        }
    }
});